package com.woniuxy.microcontentserver.messagemsg.controller;


import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.microcontentserver.bean.MessageBean;
import com.woniuxy.microcontentserver.messagemsg.service.IMessageService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Resource
    private IMessageService messageServiceImpl;

   /* @GetMapping(value = "/all" ,produces = MediaType.APPLICATION_JSON_VALUE )
    ResultMsg findAll(){
        ResultMsg resultMsg = new ResultMsg(true);
        List<AdminInfo> all = adminServiceImpl.findAll();
//        all.forEach(e -> System.out.println(e));
        resultMsg.setData(all);
        return resultMsg;
    }*/

    /**
     * 查询所有资讯
     * @param
     * @return
     */
    @GetMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findAll(){
        ResultMsg resultMsg = new ResultMsg();
        List<MessageBean> messageBeans= messageServiceImpl.findAll();
        if (messageBeans == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(messageBeans);
    }

    /**
     * 通过id查询资讯
     * @param id
     * @return
     */
    @GetMapping(value="/findbyid",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg getOneById(Long id){
        ResultMsg resultMsg = new ResultMsg();
        MessageBean messageBean=messageServiceImpl.getOneById(id);
        if (messageBean == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(messageBean);
    }

    /*@PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg save(@RequestBody RoleInfo roleInfo){
        roleServiceImpl.add(roleInfo);
        return new ResultMsg(true);
    }*/

    /**
     * 新增资讯
     * @param messageBean
     * @return
     */
    @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg addMessageBean(MessageBean messageBean){
        ResultMsg resultMsg=new ResultMsg();
        int addNew=messageServiceImpl.addMessageBean(messageBean);
        if (addNew == 0) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(addNew);
    }

  /*  @PutMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    ResultMsg delete(Integer id){
        roleServiceImpl.delete(id);
        return new ResultMsg(true);
    }*/

    /**
     * 删除资讯
     * @param id
     * @return
     */
     @PutMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg deleteMessageBean(Long id){
        ResultMsg resultMsg=new ResultMsg();
       int del= messageServiceImpl.deleteMessageBean(id);
        if (del == 0) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(del);
    }





}
